import Vue from 'vue';
import VueRouter from 'vue-router';

const VueRouterPush = VueRouter.prototype.push;
const VueRouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

VueRouter.prototype.replace = function replace(to) {
  return VueRouterReplace.call(this, to).catch((err) => err);
};
Vue.use(VueRouter);

export const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index/index.vue'),
    meta: {
      title: '卡券兑换',
    },
  },
  {
    path: '/receiptman',
    name: 'receiptman',
    component: () => import('@/views/receiptman/receiptman.vue'),
    meta: {
      title: '收件人信息',
    },
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/',
  },
];

const router = new VueRouter({
  mode: 'history',
  routes,
});

router.beforeEach((to, from, next) => {
  console.log(to, from);
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});
export default router;
